package com.gqc.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gqc.bean.Cart;
import com.gqc.service.CartService;

@Controller
public class CartController {

	@Autowired
	CartService cartService;
	@ResponseBody
	@RequestMapping("/addCart")
	public String addCart(String user,String business,String commodity,String number,Integer price,HttpSession session) {
		Integer user1 = Integer.valueOf(user);
		Integer business1 = Integer.valueOf(business);
		Integer commodity1 = Integer.valueOf(commodity);
		Integer number1 = Integer.valueOf(number);
		Integer price1 = Integer.valueOf(price);
		
		Cart cart = new Cart();
		cart.setUser(user1);
		cart.setBusiness(business1);
		cart.setCommodity(commodity1);
		cart.setNumber(number1);
		cart.setPrice(price1);
		cart.setFlag(1);
		
		
		cartService.insertCart(cart);
		List<Cart> list3 = cartService.queryCart(user1);
	
		session.setAttribute("list3size",list3.size());
		session.setAttribute("list3", list3);
		return "success";
	}
	/*
	@RequestMapping("/showCart")
	public String showCart() {
		
		
		return "showCart";
	}
	*/
	@ResponseBody
	@GetMapping("/queryCart")
	public String queryCart(@RequestParam("id")int id,HttpSession session) {
		List<Cart> list3 = cartService.queryCart(id);
		 session.setAttribute("list4", list3);
		return "success";
	}
	@RequestMapping("querybyId")
	public String querybyId(String id,Model model) {
		Integer id1 = Integer.valueOf(id);
		Cart cart = cartService.querybyId(id1);
		System.out.println("*********");
		System.out.println("cart:"+cart.getPrice());
		model.addAttribute("user",cart.getUser());
		model.addAttribute("business",cart.getBusiness());
		model.addAttribute("price",cart.getPrice());
		return "redirect:addOrder";
	}

	
}
